package wawi.kasse;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.*;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import wawi.actions.Verkauf;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.*;

public class BezahlungGUI extends JDialog
{
	private KasseController controller;
	
	public BezahlungGUI(Frame owner, KasseController controller)
	{
		super(owner);
		this.controller = controller;
		initComponents();
	}

	public BezahlungGUI(Dialog owner, KasseController controller)
	{
		super(owner);
		this.controller = controller;
		initComponents();
	}

	private void okButtonActionPerformed(ActionEvent e)
	{
		okButton.setEnabled(false);
		
		new Thread()
		{
			public void run()
			{
				int _zahlart = zahlart.getSelectedIndex();
				int _kreditkarte = Verkauf.KREDITKARTE_NONE;
				
				if (_zahlart == Verkauf.ZAHLUNG_KREDITKARTE)		
					_kreditkarte = kreditkarten.getSelectedIndex();
				
				controller.confirmZahlung(_zahlart, _kreditkarte);				
			}
		}.start();
		
	}

	private void cancelButtonActionPerformed(ActionEvent e)
	{
		controller.cancelZahlung();
	}

	private void zahlartItemStateChanged(ItemEvent e)
	{
		kreditkarteLabel.setEnabled(zahlart.getSelectedIndex()==Verkauf.ZAHLUNG_KREDITKARTE);
		kreditkarten.setEnabled(zahlart.getSelectedIndex()==Verkauf.ZAHLUNG_KREDITKARTE);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		label1 = new JLabel();
		zahlart = new JComboBox();
		zahlart.setModel(new DefaultComboBoxModel(controller.getZahlarten()));
		kreditkarteLabel = new JLabel();
		kreditkarten = new JComboBox();
		kreditkarten.setModel(new DefaultComboBoxModel(controller.getKreditkarten()));
		buttonBar = new JPanel();
		okButton = new JButton();
		cancelButton = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========
		setTitle("Bezahlung");
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(Borders.DIALOG_BORDER);
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new FormLayout(
					new ColumnSpec[] {
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC
					},
					new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC
					}));

				//---- label1 ----
				label1.setText("Zahlart w\u00e4hlen");
				contentPanel.add(label1, cc.xy(3, 3));

				//---- zahlart ----
				zahlart.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent e) {
						zahlartItemStateChanged(e);
					}
				});
				contentPanel.add(zahlart, cc.xy(5, 3));

				//---- kreditkarteLabel ----
				kreditkarteLabel.setText("Kreditkarte");
				kreditkarteLabel.setEnabled(false);
				contentPanel.add(kreditkarteLabel, cc.xy(3, 5));

				//---- kreditkarten ----
				kreditkarten.setEnabled(false);
				contentPanel.add(kreditkarten, cc.xy(5, 5));
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
				buttonBar.setLayout(new FormLayout(
					new ColumnSpec[] {
						FormFactory.GLUE_COLSPEC,
						FormFactory.BUTTON_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.BUTTON_COLSPEC
					},
					RowSpec.decodeSpecs("pref")));

				//---- okButton ----
				okButton.setText("Bezahlen");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						okButtonActionPerformed(e);
					}
				});
				buttonBar.add(okButton, cc.xy(2, 1));

				//---- cancelButton ----
				cancelButton.setText("Abbrechen");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cancelButtonActionPerformed(e);
					}
				});
				buttonBar.add(cancelButton, cc.xy(4, 1));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JLabel label1;
	private JComboBox zahlart;
	private JLabel kreditkarteLabel;
	private JComboBox kreditkarten;
	private JPanel buttonBar;
	private JButton okButton;
	private JButton cancelButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
